<taglib name="sohtml,exfield" />
<div class="toolbar-buttons">
    <if condition="$data['id']">
        <a href="{:U('Order/order_add','buyerID='.$data['id'])}" class="button lnk-add" data-window-name="orderEdit">新增订单</a><a href="{:U('Order/customer_order_list','buyerID='.$data['id'])}" class="button lnk-add" data-window-name="orderList">查看订单</a><a href="{:U('CallRecord/callrecord_of_customer_list','tel='.$data['telphone'])}" class="button lnk-add" data-window-name="cdr">通话记录</a>
    </if>
</div>
<div style="width:850px; min-height: 400px" class="crm customer-edit-page exfieldset">
    <div class="main">
        <form action="{:U('Customer/customer_edit_save')}" method="post">
            <fieldset>
                <legend>[-]基础信息</legend>
                <table class="table-form" width="100%">
                    <tr>
                        <td><th>客户编号：</th><td>{$data.serialNo}<input type="hidden" name="serialNo" value="{$data.serialNo}" /></td>
                        <td colspan="2"><th>联系电话：</th><td><input type="text" name="telphone" placeholder="电话号码" value="{$data.telphone}" required="required" data-check-url="{:U('Customer/check_telphone')}"/><span id="numberRegional" data-url="{:U('NumberRegional/get_regional','tel=valtel')}" style="color:#F00; margin-left:10px;">(号码归属地)</span></td>
                    </td></tr>
                    <tr>
                        <td><th>姓名：</th><td><input type="text" name="personalName" placeholder="客户姓名" value="{$data.personalName}" size="18"/></td>
                        <td><th>性别：</th><td><sohtml:select name="sex" items="1=先生;0=女士" value="{$data.sex}" /></td>
                    <td><th>身份证号：</th><td><input type="text" name="idNo" placeholder="身份证号码" value="{$data.idNo}"/></td>
                    </td></tr>
                    <tr>
                        <td colspan="2"><th>收货地址：</th><td><input type="text" name="address" size="50" placeholder="收货地址" value="{$data.address}" title='逗号后面保留'/><a id="addr-mgr" href="{:U('Address/address_list','uid='.$data['id'])}" style="display:none;">[管理]</a></td>
                        <td><th>邮政编码：</th><td><input type="text" name="zipCode" placeholder="邮政编码" value="{$data.zipCode}"/></td>
                    </td></tr>
                    <tr>
                        <td colspan="3"><th style="width:73px;"></th><td><sohtml:region province="{$data.province}" city="{$data.city}" area="{$data.area}" names="province,city,area" changeFunc="changeRegion" /></td>
                    </td></tr>
                    <tr>
                        <td><th>购买意向：</th><td><sohtml:select name="interested" items="0=普通;1=意向客户;2=非常兴趣" value="{$data.interested}" /></td>
                        <!--<td><th>营销结果：</th><td><sohtml:select name="contactResults" items="4=拒绝;1=成功;2=对方为老人或小孩;3=接通后即结束通话;5=接通后不说话" value="{$data.contactResults}" /></td>-->
                    <td colspan="2"><th>自定义标签：</th><td><input type="text" name="tags" value="{$data.tags}" placeholder="自定义标签,多个值以英文逗号隔开" size="52" /></td>
                    </td></tr>
                    <tr>
                        <td colspan="3"><th style="vertical-align: top">备注：</th><td><textarea name="remark" cols="85" rows="3" placeholder="备注">{$data.remark}</textarea></td></td></tr>
                </table>
            </fieldset>
            <fieldset style="width:100%;">
                <legend>[-]扩展信息</legend>
                <div style="max-height:100px; overflow-y: auto;">
                    <exfield:formfield target="CUSTOMER" siteid="{$data.siteID}" itemid="{$data.id}" class="table-form" width="100%" />
                </div>
            </fieldset><br/>
            <if condition="$upr['新增客户'] OR $upr['编辑客户']">
                <button type="submit">保存</button>
                <button type="reset">重置</button>
                <input type="reset" value="重置"/>
            </if>
            <input type='hidden' name='id' value='{$data.id}'/>
            <input type='hidden' name='siteID' value='{$data.siteID}'/>
            <input type='hidden' name='userID' value='{$data.userID}'/>
        </form>
    </div>
    <div class="aside">
        <fieldset class="summary">
            <legend>[-]消费统计：</legend>
            <div>消费次数：<strong>{$data.buyCount}</strong><br/>累计消费：<strong>{$data.cumulativeAmount}</strong><br/>上次购买：<strong>{$data.lastBuyTime}</strong></div>
        </fieldset>
        <fieldset>
            <legend>[-]来电记录：</legend>
            <div style=" max-height:180px; overflow-y: auto; line-height: 18px;" id="cdrlist"></div>
        </fieldset>
    </div>
    <div style="clear: both;"></div>
</div>
<input type="hidden" id="cdrlist-link" value="{:U('Customer/customer_edit','tel=vartel')}" />
<input type="hidden" id="cdrlist-data-url" value="{:U('XCall/ApiCallRecord/seat_get_callrecord_list','exten='.$loginData['extensionNumber'].'&uid='.$loginData['id'])}" />
<input type="hidden" id="ce-refresh-parent" value="{$rp}" />
{$script_update_dialog}
<script>
    resize_dialog(880);
    if ($('#ce-refresh-parent').val() == '1') {
        window.parent.refresh_page();
    }
    /**
     * 地区自动输入
     * @param {string} p 省份
     * @param {string} c 城市
     * @param {string} a 地区
     * @returns {undefined}
     * @since 1.0 2014-12-11 by sutroon
     * @remark 在文本框中加英文逗号,逗号后面的字串将被保留,如：福建省 厦门市 思明区, 湖滨南路(注意逗号位置)
     */
    function changeRegion(p, c, a) {
        var val = p;
        if (c) {
            val = val + ' ' + c;
        }
        if (a) {
            val = val + ' ' + a;
        }
        var $tb = $('input[name="address"]');
        var oldv = $tb.val();
        if (oldv.indexOf(',') > -1) {
            val += oldv.substring(oldv.indexOf(','));
        }
        $tb.val(val);
    }
    if ($('input[name="id"]').val()) {
        $('#addr-mgr').show();
    }
    $('#addr-mgr').click(function () {
        var opts = {'title': '地址管理', 'width': 580, 'close': function () {
                $(this).remove()
            }};
        $('<div><iframe name="addrmgr" style="width:100%; overflow:hidden;" frameborder="0" scrolling="no" src="' + $(this).attr('href') + '"></iframe></div>').dialog(opts).find('iframe').load(function () {
            var $ifrm = $(this);
            var height = $ifrm.contents().find("body").height();
            $ifrm.height(height).parent().height(height + 10);
        });
        return false;
    })
    // 选择地址 2014-12-15 by sutroon
    function set_address(personalName, telphone, address, zipCode) {
        $('input[name="personalName"]').val(personalName);
        $('input[name="telphone"]').val(telphone);
        $('input[name="address"]').val(address);
        $('input[name="zipCode"]').val(zipCode);
    }
    extension_get_callrecord_list();
    function extension_get_callrecord_list() {
        var url = $('#cdrlist-data-url').val();
        $.ajax({
            url: url,
            dataType: "jsonp",
            jsonp: "extension_callrecord_list_jsonp"
        })
    }
    // 获取历史通话记录 2014-12-16 by sutroon
    function extension_callrecord_list_jsonp(data) {
        var url = $('#cdrlist-link').val();
        var str = '';
        for (var i in data) {
            str += '<a href="' + url.replace('vartel', data[i].lineNum) + '" title="' + data[i].createdTime + '">[' + data[i].createdTime.substring(11) + '] ' + data[i].lineNum + '</a><br />';
        }
        $('#cdrlist').html(str);
    }
    // 检测电话是否重复
    $('input[name="telphone"]').blur(function () {
        var $this = $(this);
        if (!$this.val()) {
            return;
        }
        $.get($this.data('checkUrl'), {'tel': $this.val()}, function (data) {
            if (data.success != '1') {
                $.somessager.alert('提示', '电话号码已存在!');
                $this.val('');
            }
        })
    })
    // 加载归属地 2015-1-5 by sutroon
    if ($('input[name="telphone"]').val()) {
        var url = $('#numberRegional').data('url').replace('valtel', $('input[name="telphone"]').val());
        $('#numberRegional').load(url);
    }
</script>